/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2018年2月6日
 */
package org.rk.core.nosql.mongodb.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

/**
 * @author cavion
 * @描述
 * 2018年2月6日 下午1:10:22
 */
@Component
public class MongoObjectParams {
	private  String javaType = "java";
	/**
	 * @author：cavion
	 * 2018年2月6日 下午1:10:57
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public  Map<String, String> createParams(Object object) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        setIntoParams(params,object, null);
        return params;
    }
    private  void setIntoParams(Map<String, String> params,Object object, String fatherName) throws IllegalAccessException,Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field filed : fields) {
            boolean accessFlag = filed.isAccessible();
            filed.setAccessible(true);
            String name = filed.getName();
            Object value = filed.get(object);
            if(filed.getType().getName().equals("java.lang.Class")){
                break;
            }else if(filed.getType().getName().contains(javaType)){
                if(fatherName != null && !fatherName.equals(" ")){
                    name = fatherName+"."+name;
                }
                if(value != null){
                    params.put(name, value+"");
                }
            }else{
                if(value != null){
                    setIntoParams(params,filed.get(object), name);
                }
            }
            filed.setAccessible(accessFlag);
        }
    }
}
